﻿using System;
using System.Collections.Generic;
using DTO;
using DAO;
using System.Data;
using System.Data.SqlClient;

namespace BUS
{
    public class BUSHocSinh
    {
        public static DataTable LietKeKhoiLop()
        {

            DataTable dtb = DAOHocSinh.LietKeLookupEditData("sp_LietKeKhoiLop");
            return dtb;
        }

        public static DataTable LietKeLop()
        {

            DataTable dtb = DAOHocSinh.LietKeLookupEditData("sp_LietKeLop");
            return dtb;
        }

        public static int ThemHocSinh(DTOHocSinh hocsinh)
        {
            int ketqua = KiemTraThongTinHocSinh(hocsinh);
            if (ketqua == 0)
            {
                ketqua = DAOHocSinh.ThemHocSinh(hocsinh);
                return ketqua;
            }
            else
            {
                return ketqua;
            }
        }

        public static object DanhSachHocSinh()
        {
            return DAOHocSinh.DanhSachHocSinh();
        }

        public static int CapNhatThongTinHocSinh(DTOHocSinh hocsinh)
        {
            int ketqua = KiemTraThongTinHocSinh(hocsinh);
            if (ketqua == 0)
            {
                ketqua = DAOHocSinh.CapNhatThongTinHocSinh(hocsinh);
                return ketqua;
            }
            else
            {
                return ketqua;
            }
        }

        private static int KiemTraThongTinHocSinh(DTOHocSinh hocsinh)
        {
            TimeSpan ts = DateTime.Now - hocsinh.NgaySinh;
            DateTime Age = DateTime.MinValue + ts;
            if (Age.Year - 1 < GlobalVariableBUS.TuoiMin || Age.Year - 1 > GlobalVariableBUS.TuoiMax)
            {
                return -2;
            }
            if (GlobalFunction.KiemTraDoDaiChuoi(hocsinh.DienThoai, 7, 12) == false)
            {
                return -3;
            }
            if (GlobalFunction.KiemTraEmail(hocsinh.Email) == false)
            {
                return -4;
            }
            return 0;
        }

        public static int XoaHocSinh(DTOHocSinh hocsinh)
        {
            try
            {
                int kq = DAOHocSinh.XoaHocSinh(hocsinh);
                return kq;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static DataTable TraCuuHocSinh(string makhoilop, string malop, DTOHocSinh hocsinh,string gioitinh)
        {
            try
            {
                return DAOHocSinh.TraCuuHocSinh(makhoilop, malop, hocsinh,gioitinh);    
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static DataTable TraCuuHocSinh(string makhoilop, string malop, string ho, string ten, DTOHocSinh hocsinh,string gioitinh)
        {
            try
            {
                return DAOHocSinh.TraCuuHocSinh(makhoilop, malop,ho,ten, hocsinh,gioitinh);
            }
            catch (Exception ex)
            {
                throw ex;
            }    
        }

        public static DataTable LietKeLop(object p)
        {
            try
            {
                return DAOHocSinh.LietKeLop(p);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
